/*
 * @Descripttion:
 * @Author: Lizhijie
 * @Date: 2021-06-03 16:09:47
 * @LastEditTime: 2021-06-03 21:20:18
 */
/*
 * @Descripttion: 处理组织数据
 * @Author: Lizhijie
 * @Date: 2021-06-03 16:09:47
 * @LastEditTime: 2021-06-03 18:41:31
 */

import _ from 'lodash';

/**
 * @Descripttion: 获取截止到组织层级的数据
 * @param {*} list 需要筛选的数据 hrOrgTypeId 层级id
 * @return {*} 筛选后的组织list
 */
export const getFliterAfterOrg = (list, orgTypeIds) => {
  let cloneList = _.cloneDeep(list);
  if (!_.isEmpty(orgTypeIds)) {
    cloneList = _.filter(cloneList, x => orgTypeIds.includes(x.hrOrgTypeId));
  }
  return cloneList;
};

/**
 * @Descripttion: 递归组成树
 * @param {*} list
 * @param {*} pid
 * @return {*}
 */
export const buildTree = (list, pid = '00000000') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (`${x.parentOrgid}` === `${pid}`) {
      x.children = buildTree(list, x.orgdepid);
      // if (
      //   `${x.hrOrgTypeId}` !== '32' &&
      //   `${x.hrOrgTypeId}` !== '31' &&
      //   `${x.hrOrgTypeId}` !== '23'
      // ) {
      //   x.children = [];
      //   // x.selectable = false;
      //   // x.disabled = true;
      //   x.children = buildTree(list, x.orgdepid);
      // }
      tree.push({
        ...x,
        key: x.orgdepid,
        value: x.orgdepid,
        title: x.orgname,
      });
    }
  });
  return tree;
};

/**
 * @Descripttion: 找出默认选中的
 * @param {*}
 * @return {*}
 */
export function getDefaultOrgItem(list, tree = []) {
  _.each(list, x => {
    if (tree.length <= 0) {
      if (!x.disabled) {
        tree.push(x);
        return false;
      }
      getDefaultOrgItem(x.children, tree);
    }
  });
  return tree;
}

export const loopTree = (tree, lis) => {
  _.forEach(tree, x => {
    lis.push(x);
    if (x.children && x.children.length > 0) {
      loopTree(x.children, lis);
    }
  });
  return lis;
};
